/*
Assignment 1
Class ID        : SE0691
Group           : 1
Due Date        : 20 November 2012
I declare that this assignment is my own work
in accordance with FPT Policy.
*/
package edu.fpt.dal;

import edu.fpt.hib.HibernateUtil;
import edu.fpt.hib.RequestServices;
import edu.fpt.hib.RequestServicesId;
import java.util.ArrayList;
import java.util.Iterator;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Admin
 */
public class RequestServicesDAL {
     public static void deleteByUsers(String userid) {
        Session session = null;
        Transaction tran = null;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            Query query = session.createQuery("from RequestServices as rs where rs.users.userId='"+userid+"'");
            Iterator<RequestServices> it = query.iterate();
            while(it.hasNext()){
                session.delete(it.next());
            }
            tran.commit();
        } finally {
            session.close();
        }
    }
     
     public static boolean exist(RequestServicesId rsi) {
        Session session = null;
        Transaction tran = null;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            Query query = session.createQuery("from RequestServices as rs where rs.id = :id");
            query.setParameter("id", rsi);
            RequestServices user = (RequestServices) query.uniqueResult();
            tran.commit();
            return user != null;
        } finally {
            session.close();
        }
    }
     
     public static void deleteByServices(String serviceId) {
        Session session = null;
        Transaction tran = null;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            Query query = session.createQuery("from RequestServices as rs where rs.services.serviceId='"+serviceId+"'");
            Iterator<RequestServices> it = query.iterate();
            while(it.hasNext()){
                session.delete(it.next());
            }
            tran.commit();
        } finally {
            session.close();
        }
    }
     public static RequestServices getRequest(String serviceId, String userId) {
        Session session = null;
        Transaction tran = null;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            Query query = 
                    session.createQuery
                    ("from RequestServices as rs where rs.services.serviceId='"
                    +serviceId+"' and rs.users.userId='"+userId+"'");
            Iterator<RequestServices> it = query.iterate();
            tran.commit();  
            return it.next();
        } finally {
            session.close();
        }
    }
     public static void update(RequestServices entity) {
        Session session = null;
        Transaction tran = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            session.update(entity);
            tran.commit();
        } finally {
            session.close();
        }
    }
     
     public static void insert(RequestServices entity) {
        Session session = null;
        Transaction tran = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            session.save(entity);
            tran.commit();
        } finally {
            session.close();
        }
    }
     
     public static void deleteRequest(String serviceId, String userId) {
        Session session = null;
        Transaction tran = null;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            Query query = 
                    session.createQuery
                    ("from RequestServices as rs where rs.services.serviceId='"
                    +serviceId+"' and rs.users.userId='"+userId+"'");
            Iterator<RequestServices> it = query.iterate();
            while(it.hasNext()){
                session.delete(it.next());
            }
            tran.commit();
        } finally {
            session.close();
        }
    }
     
     public static ArrayList<RequestServices> listAllPeding() {
        Session session = null;
        Transaction tran = null;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            ArrayList<RequestServices> list = new ArrayList<RequestServices>();
            Query query = session.createQuery("from RequestServices where requestStatus = 'Pend'");
            list = (ArrayList<RequestServices>) query.list();
            tran.commit();
            return list;
        } finally {
            session.close();
        }
    }
     
     public static ArrayList<RequestServices> listByUser(String userid) {
        Session session = null;
        Transaction tran = null;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            ArrayList<RequestServices> list = new ArrayList<RequestServices>();
            Query query = session.createQuery("from RequestServices as rs where rs.users.userId='"+userid+"'");
            list = (ArrayList<RequestServices>) query.list();
            tran.commit();
            return list;
        } finally {
            session.close();
        }
    }
    
}
